#ifndef _PROTOHEAD_C_H_
#define _PROTOHEAD_C_H_

#ifdef __cplusplus
extern "C" {
#endif


#define CMSGHEAD_MAX_PAYLOADCOUNT	8
#define CMSGHEAD_MAX_USERDATA		64

#define CMSGHEAD_MSG_SYNC			0x99887766
#define CMSGHEAD_MSG_VERSION		0x00010101

#ifndef _ukProtoRequest
#define _ukProtoRequest				0x80000000
#endif
#ifndef _ukProtoRespond
#define _ukProtoRespond				0x40000000
#endif
#ifndef _ukProtoSet
#define _ukProtoSet					0x20000000
#endif
#ifndef _ukProtoGet
#define _ukProtoGet					0x10000000
#endif
#ifndef _ukProtoClear
#define _ukProtoClear				0x0fffffff
#endif
#ifndef _ukProtoClearMsg
#define _ukProtoClearMsg			0xf0000000
#endif

	/*
	生成协议包
	pHead = CMsgHeadNew()

	##循环使用
		Zero(pHead);
		SetCommand(pHead, _ukProtoRequest|1234);
		SetPayLoad(pHead, 100);
		pSendBuffer = Serialize(pHead);
		iSendBufferLength = ByteSize(pHead);
	##End

	CMsgHeadRelease(pHead);
	*/

	/*
	解析协议包 pNetReadBuffer iNetReadBufferBytes 网络上传输过来的数据---
		pHead = CMsgHeadNew()
		Zero(pHead);

		ParseData(pHead, pNetReadBuffer, iNetReadBufferBytes);
		while (true == bHaveMsg(pHead)){
			cmd = Command(pHead);
			switch(cmd &_ukProtoClearMsg ){
				case 1234:
					//这是上面生成的1234 指令的消息---
					for(int i = 0; i <PayLoadCount(pHead); i++){
						pData = PayLoadData(pHead, i);
						pDataLen = PayLoadLength(pHead, i);
						## pData pDataLen 便是上面的负载的数据---
					}
				break;

				default:
				break;
			}
			PopOneMsg(pHead);
		}


	##不使用的时候
		CMsgHeadRelease(pHead);

	*/






	typedef struct {
		unsigned int	_MsgSync;
		unsigned int	_MsgVersion;
		unsigned int	_MsgCommand;
		unsigned int	_MsgLength;
		unsigned short	_MsgUserDataLength;
		unsigned short	_MsgPayloadCount;
	}_MsgHeadInfo;

	typedef struct {
		unsigned short PayLoadStart;
		unsigned short PayLoadLength;
	}_MsgPayloadInfo;


	typedef struct {
		unsigned char* m_Buffer;
		unsigned int	 m_BufferSize;
		unsigned int	 m_BufferLength;
		unsigned short  m_Payload[CMSGHEAD_MAX_PAYLOADCOUNT];
		unsigned char	m_UserData[CMSGHEAD_MAX_USERDATA];
		unsigned int	m_UserDataLength;
		_MsgHeadInfo* m_MsgHead;
	}CMsgHead;

	CMsgHead* CMsgHeadNew();
	void 		CMsgHeadRelease(CMsgHead* _this);

	void		Zero(CMsgHead* _this);
	void		SetCommand(CMsgHead* _this, unsigned int Command);
	int		SetPayLoad(CMsgHead* _this, unsigned short PayLoadLength);
	int		SetUserData(CMsgHead* _this, void* Data, unsigned short length);

	int			ByteSize(CMsgHead* _this);
	char* Serialize(CMsgHead* _this);


	unsigned int Command(CMsgHead* _this);
	unsigned int Version(CMsgHead* _this);
	int		 bWithUserData(CMsgHead* _this);


	void ParseData(CMsgHead* _this, void* data, int len);
	int bHaveMsg(CMsgHead* _this);
	void PopOneMsg(CMsgHead* _this);

	void* UserData(CMsgHead* _this);
	unsigned short   UserDataLength(CMsgHead* _this);

	char* PayLoadData(CMsgHead* _this, int index);
	unsigned short   PayLoadLength(CMsgHead* _this, int index);
	unsigned short   PayLoadCount(CMsgHead* _this);




#ifdef __cplusplus
}
#endif






#endif
